/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-07-19 14:18:02
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-21 11:59:29
 * @FilePath: \ems_neutral_app\lib\pages\Mine\AccountManager\EditEmailPage.dart
 * @Description: 绑定/修改邮箱
 */
import 'package:ems_neutral_app/base/SilScaffold.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import 'package:sil_utils_package/tools/SilTools.dart';

import '../../../common/Method/CommonMethod.dart';
import '../../../common/widgets/cubitWidget/InputView.dart';
import '../../../common/widgets/cubitWidget/SubmitBtn.dart';
import '../../../utils/router/ARouter.dart';
import 'Controller/AccountManagerController.dart';

class EditEmailPage extends StatefulWidget {
  const EditEmailPage({super.key});

  @override
  State<EditEmailPage> createState() => _EditEmailPageState();
}

class _EditEmailPageState extends State<EditEmailPage> {
  AccountManagerController _accountManagerController =
      Get.put(AccountManagerController());

  String? _email;

  String? _newEmail;

  /// 验证码
  String? _code;

  /// 是否进行绑定邮箱，默认绑定
  bool _isBinding = true;

  @override
  void initState() {
    super.initState();

    _email = _accountManagerController.email.value;

    /// 判断是否进行绑定邮箱：有邮箱则是已经绑定过，现在进行修改邮箱
    if (!SilTools.stringIsEmpty(_email) && _email!.length > 0) {
      _isBinding = false;
    }
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      title: _isBinding
          ? LangOf(context)?.user?.bind_email
          : LangOf(context)?.user?.edit_email,
      body: mainWidget(),
    );
  }

  mainWidget() {
    return Container(
      margin: EdgeInsets.only(top: 40.w),
      child: Column(
        children: [
          emailNumWidget(),
          captchaWidget(),
          if (!_isBinding) newEmailWidget(),
          submitBtnWidget(),
        ],
      ),
    );
  }

  emailNumWidget() {
    return _isBinding
        ? InputView(
        margin: EdgeInsets.only(top: 30.w),
        title: LangOf(context)?.editEmail?.email,
        hintText: LangOf(context)?.editEmail?.emailphlane,
        inputViewType: InputViewType.InputViewTypePhone,
        onValid: (bool vaild, String value) {
          _email = value;
          setState(() {});
        })
        : InputView(
            title: LangOf(context)?.editEmail?.email,
            hintText: LangOf(context)?.editEmail?.emailphlane,
            inputViewType: InputViewType.InputViewTypePhone,
            value: _email,
            onClickTextField: () {},
            onValid: (bool vaild, String value) => null);
  }

  captchaWidget() {
    return InputView(
        // key: ValueKey(SilTimeUtils.currentTimeMillis()),
        margin: EdgeInsets.only(top: 30.w),
        title: LangOf(context)?.editPhone?.code,
        hintText: LangOf(context)?.editPhone?.codePhlane,
        inputViewType: InputViewType.InputViewTypeCode,
        account: _isBinding ? _newEmail : _email,
        showSendCode: true,
        showClearBtn: false,
        codeType: CodeType.CodeTypeByEmail,
        onValid: (bool vaild, String value) {
          _code = value;
        });
  }

  /// 新邮箱
  newEmailWidget() {
    return InputView(
        margin: EdgeInsets.only(top: 30.w),
        title: LangOf(context)?.editEmail?.newEmail,
        hintText: LangOf(context)?.editEmail?.newEmailPhlane,
        inputViewType: InputViewType.InputViewTypeEmail,
        onValid: (bool vaild, String value) {
          _newEmail = value;
          setState(() {});
        });
  }

  submitBtnWidget() {
    return Container(
      margin: EdgeInsets.only(top: 30.w),
      alignment: Alignment.center,
      child: SubmitBtn(
        margin: EdgeInsets.only(top: 20.w),
        title: LangOf(context)?.editNickName?.accomplish,
        onTapFunc: () {
          // 隐藏键盘
          globalTextFieldDismiss(context);
          _accountManagerController.requestEditEmail(
              context: context,
              email: _newEmail,
              code: _code,
              successHandler: (p0) => ARoute.pop(context));
        },
      ),
    );
  }
}
